using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Joy.Erp.BasicArchives.EntityFrameworkCore;

namespace Joy.Erp.BasicArchives.ItemMasters
{
    public class AuxiliaryUnitModelConfiguration : IModelConfiguration
    {
        public void Build(ModelBuilder builder)
        {
            builder.Entity<AuxiliaryUnit>(b =>
            {
                b.ToTable(BasicArchivesDbProperties.DbTablePrefix + nameof(AuxiliaryUnit), BasicArchivesDbProperties.DbSchema);
                b.ConfigureByConvention();
                b.ConfigureArchiveChildAggregateRoot();

                b.Property(x => x.UnitId).IsRequired(AuxiliaryUnitConsts.UnitIdRequired);
                b.Property(x => x.MainToAuxiliaryRatio).IsRequired(AuxiliaryUnitConsts.MainToAuxiliaryRatioRequired);
                b.Property(x => x.AuxiliaryToMainRatio).IsRequired(AuxiliaryUnitConsts.AuxiliaryToMainRatioRequired);

                b.HasOne(x => x.Unit).WithMany().HasForeignKey(x => x.UnitId).OnDelete(DeleteBehavior.Restrict);
            });
        }

        public void Entity(IAbpDbContextRegistrationOptionsBuilder options)
        {
            options.Entity<AuxiliaryUnit>(opt =>
            {
                opt.DefaultWithDetailsFunc = query =>
                {
                    return query
                        .Include(x => x.MainArchive)
                        .Include(x => x.Unit)
                        ;
                };
            });
        }
    }
}
